package com.jjs.jcenterdemo.adapterhelper;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jjs.jcenterdemo.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.white.commonlib.adapter.recycle.LinearLayoutDivider;
import com.white.commonlib.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import io.reactivex.Single;

/**
 * Created by ytf on 2018/3/24 024.
 * Description:
 */

public class QqFragment extends BaseFragment
{
    QquTopAdapter topAdapter;
    List<ListBean.SingleImgBean> topLists;

    QquListAdapter listAdapter;
    List<ListBean> lists;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recycle)
    RecyclerView recycle;

    private View topView;
    private RecyclerView topRecycle;
    private Context mContext;

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public int getContentView()
    {
        return R.layout.frag_qqu;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        topLists = new ArrayList<>();
        lists = new ArrayList<>();
    }

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator()
        {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout)
            {
                return new ClassicsHeader(context);
            }
        });
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        topView = (RecyclerView) View.inflate(view.getContext(), R.layout.header_qqu, null);
        topRecycle = (RecyclerView) topView.findViewById(R.id.top_recycle);
        refreshLayout.setOnRefreshListener(new OnRefreshListener()
        {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout)
            {
                fresh();
                refreshLayout.finishRefresh();
            }
        });
        refreshLayout.setEnableAutoLoadMore(false);
        refreshLayout.setEnableLoadMore(false);
    }

    @Override
    public void lazyLoad()
    {
        super.lazyLoad();
        refreshLayout.autoRefresh();
    }

    private static final String[] imgs = {
            "http://img0.adesk.com/download/574d99f194e5cc032325916f",
            "http://img0.adesk.com/download/574bd7b094e5cc62e0ba19d9",
            "http://img0.adesk.com/download/5752610c94e5cc4a82decbcb",
            "http://img0.adesk.com/download/574c485b94e5cc513343df90",
            "http://img0.adesk.com/download/57518e2f94e5cc1963bcd9ad",
            "http://img0.adesk.com/download/576c013494e5cc4fb6cf447b",
            "http://img0.adesk.com/download/576ca34f94e5cc2615b1731a",
            "http://img0.adesk.com/download/576d10a794e5cc132470544f",
            "http://img0.adesk.com/download/57878bf294e5cc74ebf67e34",
            "http://img0.adesk.com/download/5788289294e5cc3cd8a5446e",
            "http://img0.adesk.com/download/57df65e294e5cc0d36686836",
            "http://img0.adesk.com/download/57dfc62594e5cc527963d9f3",
            "http://img0.adesk.com/download/57dfc69594e5cc527963daf0",
            "http://img0.adesk.com/download/57e07cf794e5cc4c8af92a5e"
    };

    private void fresh()
    {
        topLists.clear();
        lists.clear();
        int size = imgs.length;
        Random rd = new Random();
        for (int i = 0; i < 10; i++)
        {
            topLists.add(new ListBean.SingleImgBean(imgs[rd.nextInt(size)]));
        }
        for (int i = 0; i < 10; i++)
        {
            ListBean l = new ListBean(imgs[rd.nextInt(size)]);
            l.itemType = rd.nextInt(2);
            if(l.itemType == 0)
            {
                l.title = "代理pos机有什么好处呢，答：代理POS机就像一家银行，天天进账";
                l.imgs = topLists;
            }
            lists.add(l);
        }

        config();
    }

    private void config()
    {
        if(topAdapter == null)
        {
            topAdapter = new QquTopAdapter(R.layout.item_qqu_top_card, topLists);
            topAdapter.setEnableLoadMore(false);
            topAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener()
            {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position)
                {
                    Toast.makeText(mContext, "header_item", Toast.LENGTH_SHORT).show();
                }
            });
        }else
        {
            topAdapter.notifyDataSetChanged();
        }

        if(listAdapter == null)
        {
            listAdapter = new QquListAdapter(lists);
            listAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener()
            {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position)
                {

                }
            });
            listAdapter.addHeaderView(topView);

            LinearLayoutManager m1 = new LinearLayoutManager(mContext, OrientationHelper.VERTICAL,false);
            recycle.setLayoutManager(m1);
            LinearLayoutDivider d1 = new LinearLayoutDivider(mContext, LinearLayoutDivider.LINEAR_V);
            d1.setDividerStyle(30, Color.TRANSPARENT);
            recycle.addItemDecoration(d1);

            recycle.setAdapter(listAdapter);

            LinearLayoutManager m = new LinearLayoutManager(mContext, OrientationHelper.HORIZONTAL,false);
            topRecycle.setLayoutManager(m);
            LinearLayoutDivider d = new LinearLayoutDivider(mContext, LinearLayoutDivider.LINEAR_H);
            d.setDividerStyle(40, Color.TRANSPARENT);
            topRecycle.setAdapter(topAdapter);
            topRecycle.addItemDecoration(d);
        }else
        {
            listAdapter.notifyDataSetChanged();
        }
    }
}
